package image;

import processing.core.PApplet;
import processing.core.PImage;

public abstract class BaseImagen extends PApplet {

	private static final long serialVersionUID = -7522752147550258088L;

	protected String url;
	protected PImage imagenOriginal;
	protected int tamX,tamY;
	protected int[][][] imagenCMYK;
	protected int[][] imagenDither; 

	public int getImageWidth() {
		return tamX;
	}
	public int getImageHeight() {
		return tamY;
	}
	
	public PImage getPImageFromBoolean(boolean[][] src) {
		PImage ret = new PImage(src.length, src[0].length);
		ret.loadPixels();
		for (int i=0;i<src.length;i++)
			for (int j=0;j<src[0].length;j++) {
				if (src[i][j])
					ret.set(i, j, color(255,255,255));
				else 
					ret.set(i, j, color(0,0,0));
			}
		ret.updatePixels();
		return ret;
	}
	
	public PImage getPImageFromDithered(int[][] src) {
		PImage ret = new PImage(src.length, src[0].length);
		ret.loadPixels();

        /*
		 * CONVERSION TABLE
		 *
		 * C=4		M=2		Y=1
		 *
		 * 		C	M	Y
		 * 0					= 0
		 * 1	X				= 4
		 * 2		X			= 2
		 * 3			X		= 1
		 * 4	X	X			= 6
		 * 5		X	X		= 3
		 * 6	X		X		= 5
		 * 7	X	X	X		= 7 negro
		 *
		 *
		 * difungir
		 *//*
		int[] paletta = new int[8];
		paletta[0] = color(255,255,255);
		paletta[1] = color(0,255,255);
		paletta[2] = color(255,0,255);
		paletta[3] = color(0,255,255);
		paletta[4] = color(0,0,255);
		paletta[5] = color(255,0,0);
		paletta[6] = color(0,255,0);
		paletta[7] = color(0,0,0);*/
		int[] paletta = new int[8];
		paletta[0] = color(0,0,0);
		paletta[1] = color(255,0,0);
		paletta[2] = color(0,255,0);
		paletta[3] = color(255,0,0);
		paletta[4] = color(255,255,0);
		paletta[5] = color(0,255,255);
		paletta[6] = color(255,0,255);
		paletta[7] = color(255,255,255);
		
		for (int i=0;i<src.length;i++)
			for (int j=0;j<src[0].length;j++) {
				ret.set(i, j, paletta[src[i][j]]);
			}
		ret.updatePixels();
		return ret;
	}
	
	
	
	protected int[] getRGBfromHex(int in) {
		int[] ret = new int[3];
		Integer i = (Integer)in;
		
		String hc = hex(i).toString();
		
		ret[0]=Integer.valueOf(hc.substring(0, 2),16);
		ret[1]=Integer.valueOf(hc.substring(2, 4),16);
		ret[2]=Integer.valueOf(hc.substring(4, 6),16);
		
		return ret;
	}
	
	
	public BaseImagen(String url) {
		this.imagenOriginal = loadImage(url);
		this.tamX = this.imagenOriginal.width;
		this.tamY = this.imagenOriginal.height;
		
		this.imagenCMYK = new int[3][this.getImageWidth()][this.getImageHeight()];
		this.imagenDither = new int[this.getImageWidth()][this.getImageHeight()];
		
	}
	
	protected abstract int[][] getDither8();
	protected abstract int[][][] getRGBtoCMY();
	
	public PImage getOriginal() {
		return this.imagenOriginal;
	}
	
	public int[][] getDithered() {
		this.imagenDither = this.getDither8();
		return this.imagenDither;
	}
	
	public void drawDithered() {
		PImage newimage = new PImage(this.getImageWidth(),this.getImageHeight());
		newimage.loadPixels();
		for (int i=0;i<this.getImageWidth();i++)
			for (int j=0;j<this.getImageHeight();j++) {
				int c = color(1,3,5);//this.imagenDither[0][i][j],this.imagenDither[1][i][j],this.imagenDither[2][i][j]);
				newimage.set(i, j, c);
			}
		newimage.updatePixels();
		image(newimage,0,0);
	}
}
